package com.lzh.others;

/**
 * @Description:
 * @Author: LiuZiHao
 * @Date: 2022/8/31 10:57
 */
public class StringBufferAndBuilder {


    StringBuffer sbuffer = new StringBuffer();
    StringBuilder sbuilder = new StringBuilder();

    public static void main(String[] args) {
        StringBufferAndBuilder main = new StringBufferAndBuilder();
        Thread1 t1 = main.new Thread1();
        new Thread(t1).start();
        new Thread(t1).start();
        new Thread(t1).start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(main.sbuffer.toString());
        System.out.println("buffer长度" + main.sbuffer.length());
        System.out.println(main.sbuilder.toString());
        System.out.println("builder长度" + main.sbuilder.length());
    }

    class Thread1 implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 2000; i++) {
                sbuffer.append("" + i % 10);
                //下面是同步块，加上同步块之后上面的StringBuffer输出和StringBuilder输出是一样的
//                synchronized (sbuilder) {
                sbuilder.append("" + i % 10);
//                }
            }
        }
    }

}
